package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.AdminInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;

@Mapper
public interface AdminMapper {
    @Select("SELECT a.*, u.real_name AS name " +
            "FROM admins a " +
            "LEFT JOIN users u ON a.user_id = u.user_id")
    List<AdminInfo> getAdminList();

    @Select("SELECT a.*, u.real_name AS name " +
            "FROM admins a " +
            "LEFT JOIN users u ON a.user_id = u.user_id " +
            "WHERE a.admin_id = #{admin_id}")
    AdminInfo getAdminById(Integer admin_id);

    @Select("<script>" +
            "SELECT a.*, u.real_name AS name FROM admins a " +
            "LEFT JOIN users u ON a.user_id = u.user_id " +
            "WHERE 1=1 " +
            "<if test='name != null'> AND u.real_name LIKE CONCAT('%', #{name}, '%') </if>" +
            "<if test='role != null'> AND a.role LIKE CONCAT('%', #{role}, '%') </if>" +
            "<if test='status != null'> AND a.status = #{status} </if>" +
            "</script>")
    List<AdminInfo> searchAdmins(
            @Param("name") String name,
            @Param("role") String role,
            @Param("status") Integer status);

    @Update("UPDATE admins SET " +
            "role = #{role}, " +
            "department = #{department}, " +
            "permissions = #{permissions}, " +
            "status = #{status} " +
            "WHERE admin_id = #{adminId}")
    int updateAdmin(@Param("role") String role,
                    @Param("department") String department,
                    @Param("permissions") String permissions,
                    @Param("status") Integer status,
                    @Param("adminId") Integer adminId);
}